import Head from "next/head";
import { useState, useEffect } from "react";
import { getMovies } from "../../services/movieService";
export default (props) => {
  const [list, setList] = useState(props.pageProps.data);
  return (
    <div>
      <Head>
        <title>电影页</title>
      </Head>
      <h1>电影页</h1>
      <ul>
        {list.map((m) => (
          <li key={m._id}>
            <a href={`/movies/${m._id}`}>
              <span>{m.name}</span>
            </a>
          </li>
        ))}
      </ul>
    </div>
  );
};

// 在服务端运行,
// 在组件渲染之前
// 该函数蜘蛛build期间运行
export async function getStaticProps() {
  const { data } = await getMovies();
  return {
    props: { data },
  };
}
